package com.itcast.activiti.service.impl;

import cn.itcast.ic.common.bean.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.activiti.entity.ActIdPassUser;
import com.itcast.activiti.mapper.ActIdPassUserMapper;
import com.itcast.activiti.service.IActIdPassUserService;
import com.itcast.activiti.service.IActIdUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2020-12-31
 */
@Service
public class ActIdPassUserServiceImpl extends ServiceImpl<ActIdPassUserMapper, ActIdPassUser> implements IActIdPassUserService {
    @Autowired
    private IActIdUserService iActIdUserService;

    @Override
    public List<Map<String, Object>> listActIdPassUserByActIdProcess(Integer actIdProcess, String userEmail) {
        QueryWrapper<ActIdPassUser> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ActIdPassUser::getActIdProcessId, actIdProcess);
        if (StringUtils.hasText(userEmail)) {
            queryWrapper.lambda().eq(ActIdPassUser::getUserEmail, userEmail);
        }
        queryWrapper.lambda().eq(ActIdPassUser::getDeleted, false);
        List<ActIdPassUser> actIdPassUsers = baseMapper.selectList(queryWrapper);
        List<Map<String, Object>> resultList = new ArrayList<>();
        actIdPassUsers.forEach(actIdPassUser -> {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", actIdPassUser.getId());
            resultMap.put("userEmail", actIdPassUser.getUserEmail());
            resultMap.put("userName", iActIdUserService.getUserNameByemail(actIdPassUser.getUserEmail()));
            resultMap.put("passUserEmail", actIdPassUser.getPassUserEmail());
            resultMap.put("passUserName", iActIdUserService.getUserNameByemail(actIdPassUser.getPassUserEmail()));
            resultList.add(resultMap);
        });
        return resultList;
    }

    @Override
    public Response addActIdPassUser(Integer actIdProcessId, List<Map<String, Object>> mapList, String createdBy) {
        //删除原有操作权限
//        QueryWrapper<ActIdPassUser> queryWrapper = new QueryWrapper();
//        queryWrapper.lambda().eq(ActIdPassUser::getActIdProcessId, actIdProcessId);
//        ActIdPassUser userRole = new ActIdPassUser();
//        userRole.setDeleted(true);
//        try {
//            baseMapper.update(userRole, queryWrapper);
//        } catch (Exception e) {
//        }
        //新增操作权限
        //去重
        Set<String> userEmails = new HashSet<>();
        for (Map<String, Object> map : mapList) {
            userEmails.add((String) map.get(ActIdPassUser.Fields.userEmail));
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (String one : userEmails) {
            for (Map<String, Object> map : mapList) {
                if (one.equals(map.get(ActIdPassUser.Fields.userEmail))) {
                    list.add(map);
                    break;
                }
            }
        }
        //新增权限
        List<ActIdPassUser> userRoleList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            String passUserEmails = (String) map.get(ActIdPassUser.Fields.passUserEmail);
            String userEmail = (String) map.get(ActIdPassUser.Fields.userEmail);
            String[] split = passUserEmails.split(",");
            for (String passUserEmail : split) {
                ActIdPassUser actIdPassUser = new ActIdPassUser();
                actIdPassUser.setActIdProcessId(actIdProcessId);
                actIdPassUser.setUserEmail(userEmail);
                actIdPassUser.setPassUserEmail(passUserEmail);
                actIdPassUser.setCreatedBy(createdBy);
                LambdaQueryWrapper<ActIdPassUser> eq = new QueryWrapper<ActIdPassUser>().lambda()
                        .eq(ActIdPassUser::getActIdProcessId, actIdProcessId)
                        .eq(ActIdPassUser::getUserEmail, userEmail)
                        .eq(ActIdPassUser::getPassUserEmail, passUserEmail)
                        .eq(ActIdPassUser::getDeleted, false);
                Integer count = baseMapper.selectCount(eq);
                if (count != 0) {
                    continue;
                }
                userRoleList.add(actIdPassUser);
            }
        }
        return Response.success(this.saveBatch(userRoleList));
    }

    @Override
    public List<ActIdPassUser> passUserEmailByEmail(Integer actIdProcessId, String email) {
        if (actIdProcessId == null || !StringUtils.hasText(email)) {
            return null;
        }
        QueryWrapper<ActIdPassUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ActIdPassUser::getActIdProcessId, actIdProcessId);
        queryWrapper.lambda().eq(ActIdPassUser::getUserEmail, email);
        queryWrapper.lambda().eq(ActIdPassUser::getDeleted, false);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean delActIdPassUser(Integer id, String deletedBy) {
        ActIdPassUser actIdPassUser = new ActIdPassUser();
        actIdPassUser.setId(id);
        actIdPassUser.setDeletedBy(deletedBy);
        actIdPassUser.setDeletedDatetime(LocalDateTime.now());
        actIdPassUser.setDeleted(true);
        int i = baseMapper.updateById(actIdPassUser);
        return i == 1;
    }
}
